<?php
/*
CSS Star Rating Snippet for MODx Content Management Framework (http://modxcms.com)
Copyright (C) 2007 Garry Nutting
Version: 2 beta

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Placeholder Values
------------------

[+starRating+] - output of the <ul> list for the star rating
[+totalVotes+] - output the total number of votes 

Snippet Parameters
------------------

&tvName = Template Variable name where vote will be stored
&useSession - Whether to use the session to restrict voting
&docID - The document ID for the star rating (ie. the ID on which you want to rate)
&viewOnly - Whether to display the star rating in 'read only' mode
&cssFile - Relative path to the star rating CSS file
&imgWidth - Width of the star image, default is 25px
&useCookie - Whether to use cookies to restrict voting
&cookieExpiry - If you are using &useCookie, you can set the expiry time using this parameter
&lang - Language (eg. english, francais) to use, the language must be available in the star_rating/lang directory
&starTpl - Top level template to use for the star rating
&webGroups - Comma separated list of valid web groups to restrict voting to
&redirect - ID of document to redirect to when voting has been cast - please make sure page is published and the user has permissions to access it.
*/
 
// include rating class
if (!class_exists('rating')) {
	include_once MODX_BASE_PATH.'assets/snippets/star_rating/rating.class.php';
}

// create rating instance
$rating = new rating;

// deprecated snippet parameters - included for backwards compatibility
if (!isset($cookieExpiry) && isset($ovtime)) $cookieExpiry = $ovtime;
if (!isset($useCookie) && isset($onevote)) $useCookie = true;

$cssFiles = array(
	'1' => 'assets/snippets/star_rating/star.css'
);

if (isset($cssFile) && array_key_exists($cssFile,$cssFiles)) {
	$cssFile = $cssFiles[$cssFile];
}

// snippet parameters
$rating->templateVar = (isset ($tvName)) ? $tvName : 'star_rating';
$rating->useSession = (isset ($useSession)) ? true : false;
$rating->documentId = (isset ($docID)) ? $docID : $modx->documentObject['id'];
$rating->passedId = (isset ($_GET['pid'])) ? $_GET['pid'] : '';
$rating->viewOnly = (isset ($viewOnly)) ? true : false;
$rating->cssFile = (isset ($cssFile)) ? $modx->config['site_url'].$cssFile : $modx->config['site_url'].'assets/snippets/star_rating/star.css';
$rating->imgWidth = (isset ($imgWidth)) ? $imgWidth : 25;
$rating->useCookie = (isset ($useCookie)) ? true : false;
$rating->cookieExpiry = (isset ($cookieExpiry)) ? $cookieExpiry : 608400;
$rating->lang = (isset ($lang)) ? $lang : false;
$rating->starTpl = isset ($starTpl) ? $modx->getChunk($starTpl) : '[+starRating+]<span class="totalvotes">Votes: [+totalVotes+]</span>';
$rating->webGroups = isset($webGroups) ? $webGroups : false;
$rating->redirect = isset($redirect) ? $redirect : $modx->documentObject['id'];

// process
$rating->outputCSS();
$rating->setVote(); // will process postback if required
return $rating->renderVote();
?>